#include "players_list.h"

#include <qpainter.h> 

/*
*
*  class PlayersListItem implementation
*
*/

const char * PlayersListItem::columnsText[] = {"Color","Player","Dots","Won dots","Lost dots"};
const int PlayersListItem::columnWidth[] = {33,60,-1,-1,-1};

PlayersListItem::PlayersListItem(QListView * parent) 
: QListViewItem (parent)
{
}

PlayersListItem::~PlayersListItem() {
}

PlayerProfile * PlayersListItem::getProfile() {
	return profile;
}

void PlayersListItem::setProfile (PlayerProfile * p) {
	profile = p;
}

int PlayersListItem::getColumnCount () {
	return countof(columnsText); ///XXX
}

QString PlayersListItem::getColumnText (int i) {
	return columnsText[i];
}

int PlayersListItem::getColumnWidth (int i) {
	return columnWidth [i];
}

void 
PlayersListItem::paintCell ( QPainter * p, const QColorGroup & cg, int column, int width, int align )
{
	int t;
	char str[20];
	QListViewItem::paintCell (p, cg, column, width, align);
	switch (column) {
		case 0:
			p->fillRect (0, 0, width, this->height(), QBrush (QRgb(profile->color)));
			break;
		case 1:
			p->translate (0, height ());
			p->drawText (0, 0, QString(profile->name));
			break;
		case 2:
			p->translate (0, height ());
			memset (str, 0, 20);
			sprintf (str, "%d", profile->stat.dots);
			p->drawText (0, 0, QString(str));
			break;
		case 3:
			p->translate (0, height ());
			memset (str, 0, 20);
			sprintf (str, "%d", profile->stat.dotsWin);
			p->drawText (0, 0, QString(str));
			break;
		case 4:
			p->translate (0, height ());
			memset (str, 0, 20);
			sprintf (str, "%d", profile->stat.dotsLost);
			p->drawText (0, 0, QString(str));
			break;
	}
}



/*
*
*  class PlayersList implementation
*
*/

PlayersList::PlayersList (GameController * _c, QWidget * parent, const char * name, WFlags f) 
	: QListView (parent, name, f), controller (_c)
{
	this->setHScrollBarMode (ScrollBarMode::AlwaysOff);
	this->setResizePolicy (QScrollView::Manual);
	this->setSorting (-1);
	this->setSelectionMode (QListView::SelectionMode::Single);
	reset ();
	int k = PlayersListItem::getColumnCount (), i;
	for (i = 0; i <= k; i++) {
		int w = PlayersListItem::getColumnWidth(i);
		if (-1 == w)
			this->addColumn (PlayersListItem::getColumnText(i));
		else
			this->addColumn (PlayersListItem::getColumnText(i), w);
	}
}

void PlayersList::reset () {
	this->clear ();
}

void PlayersList::updateList () {	
	//check status, enumerate and call update ()
	reset ();
	Game * game = controller->getCurrentGame ();
	if (!game)
		return;
	int count = game->getPlayersCount();
	for (int i = 0; i < count; i++) {
		PlayerProfile * p = game->getPlayer (i);
		PlayersListItem * item = new PlayersListItem (this);
		item->setProfile (p);
		this->insertItem (item);
	}
	//repaint ();
}

void PlayersList::setGameController (GameController * _game) {
	this->controller = _game;
	updateList ();
}

PlayersList::~PlayersList () {
}


QSizePolicy PlayersList::sizePolicy() const {
	return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding);
}

QSize PlayersList::sizeHint () const {
	return QSize(250, 100);
}
